# 多进程: 传递参数
# 通过args方式传递参数 注意顺序
# 通过kwargs方式传递参数 指明键值对

import time
import multiprocessing
import os


def coding(name, num):
    for i in range(num):
        print(f'{name}正在编写第{i}行代码')
        time.sleep(0.2)


def music(name, num):
    for i in range(num):
        print(f'{name}正在听第{i}首音乐')
        time.sleep(0.2)


if __name__ == '__main__':
    print('多进程形式, 一边代码一边音乐')

    # 通过args方式传递参数 注意顺序
    # p1 = multiprocessing.Process(target=coding, args=("小明", 10))
    # p2 = multiprocessing.Process(target=music, args=("小明", 10))

    # 通过kwargs方式传递参数 指明键值对
    p1 = multiprocessing.Process(target=coding, kwargs={"name":"小明", "num":10})
    p2 = multiprocessing.Process(target=music, kwargs={"name":"大暗", "num":10} )

    print('p1-->', p1)
    print('p2-->', p2)

    t1 = time.time()

    p1.start() #
    p2.start()

    print('aaaa')
    p1.join()
    print('bbbb')
    p2.join()
    print('cccc')

    t2 = time.time()
    print(f'用时{t2-t1}')
    pass